"use strict";

var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _regeneratorRuntime2 = _interopRequireDefault(require("@babel/runtime/helpers/regeneratorRuntime"));

var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator"));

var _react = require("react");

var _useRefFunction = require("../useRefFunction");

function useDebounceFn(fn, wait) {
  var callback = (0, _useRefFunction.useRefFunction)(fn);
  var timer = (0, _react.useRef)();
  var cancel = (0, _react.useCallback)(function () {
    if (timer.current) {
      clearTimeout(timer.current);
      timer.current = null;
    }
  }, []);
  var run = (0, _react.useCallback)( /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee2() {
    var _len,
        args,
        _key,
        _args2 = arguments;

    return (0, _regeneratorRuntime2.default)().wrap(function _callee2$(_context2) {
      while (1) {
        switch (_context2.prev = _context2.next) {
          case 0:
            for (_len = _args2.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
              args[_key] = _args2[_key];
            }

            if (!(wait === 0 || wait === undefined)) {
              _context2.next = 3;
              break;
            }

            return _context2.abrupt("return", callback.apply(void 0, args));

          case 3:
            cancel();
            return _context2.abrupt("return", new Promise(function (resolve) {
              timer.current = setTimeout( /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/(0, _regeneratorRuntime2.default)().mark(function _callee() {
                return (0, _regeneratorRuntime2.default)().wrap(function _callee$(_context) {
                  while (1) {
                    switch (_context.prev = _context.next) {
                      case 0:
                        _context.t0 = resolve;
                        _context.next = 3;
                        return callback.apply(void 0, args);

                      case 3:
                        _context.t1 = _context.sent;
                        (0, _context.t0)(_context.t1);

                      case 5:
                      case "end":
                        return _context.stop();
                    }
                  }
                }, _callee);
              })), wait);
            }));

          case 5:
          case "end":
            return _context2.stop();
        }
      }
    }, _callee2);
  })), [callback, cancel, wait]);
  (0, _react.useEffect)(function () {
    return cancel;
  }, [cancel]);
  return {
    run: run,
    cancel: cancel
  };
}

var _default = useDebounceFn;
exports.default = _default;